﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Business.Entity;

namespace Business.Dal
{
    public class ResimProvider : Provider<Resim>
    {

        public override bool Insert(Resim tObject)
        {
            bool returnValue = false;
            
            SqlCommand cmd = new SqlCommand("ResimEkle");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            cmd.Parameters.Add("@IlanID", SqlDbType.Int).Value = tObject.IlanID;
            cmd.Parameters.Add("@ServerFileName", SqlDbType.VarChar).Value = tObject.ServerFileName;
            tObject.ResimID = Convert.ToInt32(cmd.ExecuteScalar());
            returnValue = true;

            return returnValue;


        }

        public override bool Update(Resim tObject)
        {
            bool returnValue = false;
            SqlCommand cmd = new SqlCommand("ResimGuncellexxxxx");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            cmd.Parameters.Add("@ResimID", SqlDbType.Int).Value = tObject.ResimID;
            cmd.Parameters.Add("@IlanID", SqlDbType.Int).Value = tObject.IlanID;
            cmd.Parameters.Add("@ServerFileName", SqlDbType.Int).Value = tObject.ServerFileName;
            returnValue = Convert.ToBoolean(cmd.ExecuteScalar());
            return returnValue;
        }

        public override bool Delete(int RecordID)
        {
            bool returnValue = false;
            SqlCommand cmd = new SqlCommand("ResimSil");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@ResimID", SqlDbType.Int).Value = RecordID;
            cmd.Connection = Tools.Connection();
            Tools.Open();

            

            returnValue = Convert.ToBoolean(cmd.ExecuteNonQuery());

            return returnValue;
        }

        public List<Resim> ResimGetirIlanID(int IlanID)
        {
            List<Resim> returnValue = new List<Resim>();
            SqlCommand cmd = new SqlCommand("ResimGetirIlanID");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            cmd.Parameters.Add("@IlanID", SqlDbType.Int).Value = IlanID;
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Resim r = new Resim();
                r.ResimID = dr["ResimID"] is DBNull ? 0 : Convert.ToInt32(dr["ResimID"]);
                r.IlanID = dr["IlanID"] is DBNull ? 0 : Convert.ToInt32(dr["IlanID"]);
                r.ServerFileName = dr["ServerFileName"] is DBNull ? String.Empty : dr["ServerFileName"].ToString();
                returnValue.Add(r);
            }

            dr.Dispose();
            return returnValue;
            
        }

        
    }
}
